/**
 * 文件名：****.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：*****
 */
package com.lightkits.wms.domain.usuallyinterface;

import com.lightkits.common.TransactionValidate;
import com.lightkits.common.db.entity.FndOrganizationBase;
import com.lightkits.common.db.entity.FndWmMoveTypeBase;
import com.lightkits.framework.common.logging.Logger;
import com.lightkits.framework.common.logging.LoggerFactory;
import com.lightkits.framework.common.utility.json.JsonUtil;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.soa.hzero.executor.ServiceExecutor;
import com.lightkits.wms.database.dao.WmsFndWmMoveTypeBaseMapper;
import com.lightkits.wms.database.model.SimWmMoveTypeModel;
import com.lightkits.wms.dto.usuallyinterface.GetSimMoveTypeRequestType;
import com.lightkits.wms.dto.usuallyinterface.GetSimMoveTypeResponseType;
import com.lightkits.wms.dto.usuallyinterface.model.SimpleMoveTypeDTO;
import com.lightkits.wms.util.models.MoveTypeControl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 获取移动类型
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/4/21 15:57
 */
@Component
public class GetSimMoveTypeExecutor implements ServiceExecutor<GetSimMoveTypeRequestType, GetSimMoveTypeResponseType> {
    private final WmsFndWmMoveTypeBaseMapper moveTypeBaseMapper;
    private static final Logger LOGGER = LoggerFactory.getLogger(GetWmMoveTypeExecutor.class);
    private static final String SUB = "SUB";
    private static final String MIS = "MIS";

    @Autowired
    public GetSimMoveTypeExecutor(WmsFndWmMoveTypeBaseMapper moveTypeBaseMapper) {
        this.moveTypeBaseMapper = moveTypeBaseMapper;
    }

    @Override
    public GetSimMoveTypeResponseType service(GetSimMoveTypeRequestType request) {
        GetSimMoveTypeResponseType response = new GetSimMoveTypeResponseType();
        response.setSuccessful(true);
        //获取移动类型参数校验
        TransactionValidate check1 = TransactionValidate.beginValidity(FndOrganizationBase.class);
        check1.checkIdValidity(ObjectUtil.toString(request.getOrgId()), "ORGANIZATION_ID");

        check1 = TransactionValidate.beginValidity(FndWmMoveTypeBase.class);
        check1.checkIdValidity(ObjectUtil.toString(request.getMoveType()), "WM_TYPE");
        //获取移动类型
        List<SimWmMoveTypeModel> moveTypeModels =
                moveTypeBaseMapper.queryWmMoverTypeByParams(
                        request.getOrgId(), request.getMoveType());
        response.setSimpleMoveTypeDTOS(this.convertDto(moveTypeModels, request.getType()));
        return response;
    }

    /**
     * 数据类型转换
     *
     * @param models 移动类型model
     * @param type   类型（判断是库存转移还是杂项调整）
     * @return SimpleMoveTypeDTO
     **/
    private List<SimpleMoveTypeDTO> convertDto(List<SimWmMoveTypeModel> models, String type) {
        List<SimpleMoveTypeDTO> dtos = new ArrayList<>();
        for (SimWmMoveTypeModel model : models) {
            MoveTypeControl moveTypeControl = JsonUtil.parse(model.getMoveTypeControl(), MoveTypeControl.class);
            if (!ObjectUtil.isNullOrEmpty(type)) {
                if (type.equals(SUB)) {
                    boolean flag = moveTypeControl.isTRANS_IN_ORG();
                    if (!flag) {
                        continue;
                    }
                }
                if (type.equals(MIS)) {
                    boolean flag = moveTypeControl.isINVENTORY_ADJUST();
                    if (!flag) {
                        continue;
                    }
                }
            }
            SimpleMoveTypeDTO dto = new SimpleMoveTypeDTO();
            dto.setWmMoveTypeId(model.getWmMoveTypeId());
            dto.setWmMoveType(model.getWmMoveType());
            dto.setWmMoveTypeCode(model.getWmMoveTypeCode());
            dto.setWmMoveTypeDesc(model.getWmMoveTypeDesc());
            dto.setWmMoveTypeName(model.getWmMoveTypeName());
            dtos.add(dto);
        }
        return dtos;
    }
}
